// pages/systemMessage/index.js
import api from '../../request/api.js';
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    pageNum:1,
    pageSize:10,
    isEnd:false,
  },
  async getList(pageNum){
    var that = this;
    let user_id = wx.getStorageSync('userInfo').user_id;
    const { data: res } = await api('GET', 'Recycle/other/getSystemMessageList',{
      token: wx.getStorageSync('token'),
      pageNum:pageNum,
      pageSize:that.data.pageSize,
      user_id : user_id
    })
    if(res.code ===200){
      //判断是否存在下一页
      if(res.data.length <= 10 && that.data.pageNum > 1){
        that.setData({
          isEnd:true
        })
      }
      if(res.data.length > 0){
        that.setData({
          pageNum:that.data.pageNum+1
        })
      }
      that.setData({
        list: res.data.messageList
      })
    }
    
  },
  goDetail(e) {
    const id = e.currentTarget.dataset.id;
    const read = e.currentTarget.dataset.read;
    wx.navigateTo({
      url: '../messageDetail/index?id='+id+'&read='+read,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    that.getList(1);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if(that.data.isEnd){
      console.error('到底了');return;
    }
    that.getList(that.data.pageNum);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})